clear all
clc
%close all
load dtPortfolio_HOSE_20_1.mat  
risk_free = 0;
cost = 0.03;
short_MA_length = 1;
long_MA_length = 25;
band = 0.01;
n_short_MA_length = length(short_MA_length);
n_long_MA_length  = length(long_MA_length);
n_band            = length(band);
    
test_option = 's';
%test_option = 'e';
close_price = portfolio_close_price_t(:,2:end) * porfolio_weights_t;
%close_price = close_price(180:300);
for i = 1:n_short_MA_length
    for j = 1:n_long_MA_length
        for k = 1:n_band
            figure;
            [position_legend,~,rBuyMA,rSellMA] = testVLMA(close_price, risk_free, cost, short_MA_length(i), long_MA_length(j), band(k), test_option,[]);
            axis([100 240 15 21])
            %axis square
            str = {'Short MA';'Long MA';'Upper band';'Lower band';'Buy signals';'Sell signals';'Receive stocks T+3';'Sell stocks';'Trading signals'};
            [legend_h,object_h,plot_h,text_strings] = legend(position_legend,str,'location','eastoutside');
            %CLegend(plot_h,5,'location','southoutside')
            title(['Variable Length Moving Average Trading Rule ('...
                ,num2str(short_MA_length),',',num2str(long_MA_length)...
                ,',',num2str(band),')'],'FontWeight','bold');
            xlabel('Days')
            ylabel('Price (thousand VND)')
            figure;
            position_legend = testFLMA(close_price, risk_free, cost, short_MA_length(i), long_MA_length(j), band(k), test_option,[]);
            axis([100 240 15 21])
            %axis square
            str = {'Short MA';'Long MA';'Upper band';'Lower band';'Buy signals';'Sell signals';'Receive stocks T+3';'Sell stocks';'Trading signals'};
            [legend_h,object_h,plot_h,text_strings] = legend(position_legend,str,'location','eastoutside');
            %CLegend(plot_h,4,'location','southoutside')
            title(['Fixed Length Moving Average Trading Rule ('...
                ,num2str(short_MA_length),',',num2str(long_MA_length)...
                ,',',num2str(band),')'],'FontWeight','bold');
            xlabel('Days')
            ylabel('Price (thousand VND)')
            figure;
            position_legend = testTRB(close_price, risk_free, cost, [], [], band(k), test_option, long_MA_length(j));
            axis([100 240 15 21])
            %axis square
            str = {'Close price';'Local Maximum';'Local Minimum';'Buy signals';'Sell signals';'Receive stocks T+3';'Sell stocks';'Trading signals'};
            [legend_h,object_h,plot_h,text_strings] = legend(position_legend,str,'location','eastoutside');
            %CLegend(plot_h,4,'location','southoutside')
            title(['Trading Range Break-out Rule ('...
                ,num2str(short_MA_length),',',num2str(long_MA_length)...
                ,',',num2str(band),')'],'FontWeight','bold');
            xlabel('Days')
            ylabel('Price (thousand VND)')
        end
    end
end
figure;
model = 'AR1';
[position_legend,~,rBuyAR,rSellAR] = testTimeSeries2(close_price,model);
axis([100 240 15 21])
%axis square
str = {'Close price';'Buy signals';'Sell signals'};
[legend_h,object_h,plot_h,text_strings] = legend(position_legend,str,'location','eastoutside');
%CLegend(plot_h,4,'location','southoutside')
title('Time series forecast AR(1)','FontWeight','bold');
xlabel('Days')
ylabel('Price (thousand VND)')  

figure;
position_legend = combine(close_price,rBuyMA,rSellMA,rBuyAR,rSellAR);
axis([100 240 15 21])
%axis square
str = {'Close price';'Buy signals';'Sell signals'};
[legend_h,object_h,plot_h,text_strings] = legend(position_legend,str,'location','eastoutside');
%CLegend(plot_h,4,'location','southoutside')
title(['Combined trading strategies AR(1) - VMA('...
    num2str(short_MA_length),',',num2str(long_MA_length)...
    ,',',num2str(band),')'],'FontWeight','bold');
xlabel('Days')
ylabel('Price (thousand VND)') 